package com.centersoft.flowable.core.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.centersoft.flowable.core.dto.QueryAuditDocumentDto;
import com.centersoft.flowable.core.dto.TaskBackDto;
import com.centersoft.flowable.core.model.ProcessVo;
import com.centersoft.flowable.core.model.QueryUserByDepIdRequest;
import com.centersoft.flowable.core.model.QueryUserByRoleIdRequest;
import com.centersoft.flowable.core.model.StartProcessRequest;
import com.centersoft.flowable.core.web.service.FlowHistoryService;
import com.centersoft.flowable.core.web.service.FlowTaskService;
import com.centersoft.flowable.core.comm.JsonResult;
import com.centersoft.flowable.core.dto.TaskAuditDto;
import com.centersoft.flowable.core.dto.TaskStopDto;
import com.centersoft.flowable.core.model.*;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.*;

@RestController
@RequestMapping("/flowable/task")
@ConditionalOnProperty(prefix = "workflow", value = "enableNewController", havingValue = "true", matchIfMissing = true)
public class FlowTaskController {


    private final FlowTaskService workFlowService;
    private final FlowHistoryService flowHistoryService;

    public FlowTaskController(FlowTaskService workFlowService, FlowHistoryService flowHistoryService) {
        this.workFlowService = workFlowService;
        this.flowHistoryService = flowHistoryService;
    }


    @RequestMapping(value = "/getUserByRole")
    @ResponseBody
    public JsonResult<List<String>> getUserByRoleId(@RequestBody @Valid QueryUserByRoleIdRequest request) {
        return JsonResult.success("", workFlowService.getUserByRoleId(request));
    }

    @RequestMapping(value = "/getUserByDept")
    @ResponseBody
    public JsonResult<List<String>> getUserByDeptId(@RequestBody @Valid QueryUserByDepIdRequest request) {
        return JsonResult.success("", workFlowService.getUserByDepId(request));
    }

    @RequestMapping("/startProcess")
    public JsonResult<?> startProcess(@RequestBody @Valid StartProcessRequest reviewRequest) {
        workFlowService.startProcess(reviewRequest);
        return JsonResult.success("提交审核成功");
    }


    @RequestMapping(value = "/getHistoryTaskListAndFlowChart")
    public Object getHistoryTaskListAndFlowChart(String procInsId, String orderBillcode, String isApp, String proMark) {

        return null;
    }


    @RequestMapping("/agree")
    public JsonResult<?> agree(@RequestBody TaskAuditDto agreeDTo) {
        workFlowService.agree(agreeDTo);
        return JsonResult.success("审核成功");
    }

    @RequestMapping("/stop")
    public JsonResult<?> stop(@RequestBody TaskStopDto agreeDTo) {
        workFlowService.stop(agreeDTo);
        return JsonResult.success("审核成功");
    }

    @RequestMapping("/reject")
    public JsonResult<?> reject(@RequestBody TaskBackDto backDto) {
        workFlowService.back(backDto);
        return JsonResult.success("审核成功");
    }


    @RequestMapping("/getTodoList")
    public JsonResult<List<ProcessVo>> getAuditListByEmid(String menuCode, String emid) {
        List<ProcessVo> auditListByEmid = workFlowService.getAuditListByEmid1(menuCode, emid);
        return JsonResult.success("获取代办成功", auditListByEmid);
    }

    @RequestMapping("/getAppTodoList")
    public JsonResult<List<ProcessVo>> getAppTodoList(String menuCode, String emid) {
        List<ProcessVo> auditListByEmid = workFlowService.getAppAuditListByEmid1(menuCode, emid);
        return JsonResult.success("获取代办成功", auditListByEmid);
    }

    @RequestMapping("/getMyStartedProcIns")
    public JSONObject getMyStartedProcIns(Integer pageIndex) {
        return null;
    }

    @RequestMapping("/getMyUserAppDoneTask")
    public JSONObject getMyUserAppDoneTask(Integer pageIndex) {
        return null;
    }


    @RequestMapping("getAuditDocumentNewForClient")
    public JsonResult<Object> getAuditDocumentNewForClient(@RequestBody QueryAuditDocumentDto request) {
        Object auditDocumentNewForClient = flowHistoryService.getAuditDocumentNewForClient(request);
        return JsonResult.success("查询成功", auditDocumentNewForClient);
    }

}
